<?php
namespace app\index\controller;

use think\Controller;

class MysqlPdo
{
    // protected  $pdo;
     protected $slave;  //主 
     protected  $mysql2;  //从
       protected $reids;  //redis
    public function __construct()
    {
           $this->pdo= new \PDO('mysql:host=localhost;dbname=009', 'root','root');
           $this->reids=new \Redis();
           $this->reids->connect('127.0.0.1','6379');
    }
    //添加
    public function add($arr)
    {
        $this->pdo= new \mysql2('mysql:host=localhost;dbname=009', 'root','root');
          $sql="insert into book values(null,'{$arr['name']}','{$arr['bid']}','{$arr['title']}')";

          return $this->pdo->exec($sql);
    }
    //展示+分页+搜索+redis缓存
//    public function list($page,$name)
    {
//        $this->pdo= new \slave('mysql:host=localhost;dbname=009', 'root','root');
//          $limit=6;
//          $start=($page-1)*$limit;
//
//          //print_r($limit);die;
//
//          if(!empty($name)){
//                      $query=$this->reids->get($name);
//
//                      if($query){
//                               echo "这是redis缓存";
//                               return json_decode($query,true);
//                      }
//
//              $sql="select * from book where name like '%$name%' limit $limit,$start";
//          }else{
//              $sql="select * from book limit $limit,$start";
//          }
//        //$sql="select * from book where name like '%$name%'";
//            $res=$this->pdo->query($sql);
//             return $data=$res->fetchAll(2);
//
//          if(!empty($name)){
//               $query=$this->reids->set($name);
//          }
//          return $data;
//
 }

    //删除
    public function del($id)
    {
        $this->pdo= new \mysql1('mysql:host=localhost;dbname=009', 'root','root');
        $sql="delete from book where id=".$id;

        return $this->pdo->exec($sql);
    }
    //修改
    public function updat()
    {
        $this->pdo= new \mysql1('mysql:host=localhost;dbname=009', 'root','root');
        $sql="update book set name=''where=id";

        return $this->pdo->exec($sql);
    }
}